package com.example.hu.UI;

import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Switch;
import android.widget.TextView;

import com.csht.facelib.utils.SpUtils;
import com.csht.facelib.utils.ToastUtils;
import com.example.hu.R;

import java.math.BigDecimal;

public class FaceSettingActivity extends BaseActivity {

    private Switch s_jxcl,s_xjjx,s_rgb_live,s_quality_check,s_jxjc;
    private RadioGroup rg_cam_cs,rg_face_angle, rg_cam_angle;
    private TextView tv_scale_value,tv_dis_value, tv_more_value,tv_facelf_value,tv_faceud_value;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_face_setting);

        init();
        initData();
        initClick();
    }

    private void initClick() {
        findViewById(R.id.iv_left_more).setOnClickListener(this);
        findViewById(R.id.iv_right_more).setOnClickListener(this);
        findViewById(R.id.iv_left_dis).setOnClickListener(this);
        findViewById(R.id.iv_right_dis).setOnClickListener(this);
        findViewById(R.id.iv_left_facelf).setOnClickListener(this);
        findViewById(R.id.iv_right_facelf).setOnClickListener(this);
        findViewById(R.id.iv_left_faceud).setOnClickListener(this);
        findViewById(R.id.iv_right_faceud).setOnClickListener(this);
        findViewById(R.id.iv_left_scale).setOnClickListener(this);
        findViewById(R.id.iv_right_scale).setOnClickListener(this);
        s_rgb_live.setOnCheckedChangeListener(this);
        s_xjjx.setOnCheckedChangeListener(this);
        s_jxcl.setOnCheckedChangeListener(this);
        s_quality_check.setOnCheckedChangeListener(this);
        s_jxjc.setOnCheckedChangeListener(this);
        rg_face_angle.setOnCheckedChangeListener(this);
        rg_cam_cs.setOnCheckedChangeListener(this);
        rg_cam_angle.setOnCheckedChangeListener(this);
    }

    private void initData() {
        s_jxjc.setChecked(SpUtils.isOpenMirror());
        s_quality_check.setChecked(SpUtils.isQualityControl());
        s_rgb_live.setChecked(SpUtils.isOpenLive());
        s_xjjx.setChecked(SpUtils.isCameraMirror());
        s_jxcl.setChecked(SpUtils.isDataMirror());
        ((RadioButton) findViewById(R.id.rb_cam_cs1)).setChecked(SpUtils.getCameraWidth() == 640);
        ((RadioButton) findViewById(R.id.rb_cam_cs2)).setChecked(SpUtils.getCameraWidth() == 480);
        ((RadioButton) findViewById(R.id.rb_face1)).setChecked(SpUtils.getDetectDirection() == 0);
        ((RadioButton) findViewById(R.id.rb_face2)).setChecked(SpUtils.getDetectDirection() == 90);
        ((RadioButton) findViewById(R.id.rb_face3)).setChecked(SpUtils.getDetectDirection() == 180);
        ((RadioButton) findViewById(R.id.rb_face4)).setChecked(SpUtils.getDetectDirection() == 270);
        ((RadioButton) findViewById(R.id.rb_cam1)).setChecked(SpUtils.getVideoDirection() == 0);
        ((RadioButton) findViewById(R.id.rb_cam2)).setChecked(SpUtils.getVideoDirection() == 90);
        ((RadioButton) findViewById(R.id.rb_cam3)).setChecked(SpUtils.getVideoDirection() == 180);
        ((RadioButton) findViewById(R.id.rb_cam4)).setChecked(SpUtils.getVideoDirection() == 270);
        tv_more_value.setText(String.valueOf(SpUtils.getThreshold()));
        tv_facelf_value.setText(String.valueOf(SpUtils.getMoveLeftOrRight()));
        tv_faceud_value.setText(String.valueOf(SpUtils.getMoveUpOrDown()));
        tv_scale_value.setText(String.valueOf(SpUtils.getFaceScale()));
        showFaceDistance(SpUtils.getFaceDistance());
    }

    private void init() {
        s_rgb_live = findViewById(R.id.s_rgb_live);
        s_xjjx = findViewById(R.id.s_xjjx);
        s_jxcl = findViewById(R.id.s_jxcl);
        s_quality_check = findViewById(R.id.s_quality_check);
        s_jxjc = findViewById(R.id.s_jxjc);
        rg_face_angle = findViewById(R.id.rg_face_angle);
        rg_cam_cs = findViewById(R.id.rg_cam_cs);
        rg_cam_angle = findViewById(R.id.rg_cam_angle);
        tv_more_value = findViewById(R.id.tv_more_value);
        tv_scale_value = findViewById(R.id.tv_scale_value);
        tv_dis_value = findViewById(R.id.tv_dis_value);
        tv_facelf_value = findViewById(R.id.tv_facelf_value);
        tv_faceud_value = findViewById(R.id.tv_faceud_value);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, final int checkedId) {
        int id = group.getId();
        if (id == R.id.rg_cam_cs){
            if (checkedId == R.id.rb_cam_cs1) {
                SpUtils.setCameraWidth(640);
                SpUtils.setCameraHeight(480);
            } else if (checkedId == R.id.rb_cam_cs2) {
                SpUtils.setCameraWidth(480);
                SpUtils.setCameraHeight(640);
            }
        } else if (id == R.id.rg_face_angle) {
            if (checkedId == R.id.rb_face1) {
                SpUtils.setDetectDirection(0);
            } else if (checkedId == R.id.rb_face2) {
                SpUtils.setDetectDirection(90);
            } else if (checkedId == R.id.rb_face3) {
                SpUtils.setDetectDirection(180);
            } else if (checkedId == R.id.rb_face4) {
                SpUtils.setDetectDirection(270);
            }
        } else if (id == R.id.rg_cam_angle) {
            if (checkedId == R.id.rb_cam1) {
                SpUtils.setVideoDirection(0);
            } else if (checkedId == R.id.rb_cam2) {
                SpUtils.setVideoDirection(90);
            } else if (checkedId == R.id.rb_cam3) {
                SpUtils.setVideoDirection(180);
            } else if (checkedId == R.id.rb_cam4) {
                SpUtils.setVideoDirection(270);
            }
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        int id = buttonView.getId();
        switch (id) {
            case R.id.s_quality_check:
                SpUtils.setQualityControl(isChecked);
                break;
            case R.id.s_rgb_live:
                SpUtils.setOpenLive(isChecked);
                break;
            case R.id.s_jxjc:
                SpUtils.setOpenMirror(isChecked);
                break;
            case R.id.s_xjjx:
                SpUtils.setCameraMirror(isChecked);
                break;
            case R.id.s_jxcl:
                SpUtils.setDataMirror(isChecked);
                break;
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.iv_left_more:
                moreChange(false);
                break;
            case R.id.iv_right_more:
                moreChange(true);
                break;
            case R.id.iv_left_dis:
                disChange(false);
                break;
            case R.id.iv_right_dis:
                disChange(true);
                break;
            case R.id.iv_left_facelf:
                moveLeftOrRight(false);
                break;
            case R.id.iv_right_facelf:
                moveLeftOrRight(true);
                break;
            case R.id.iv_left_faceud:
                moveUpOrDown(false);
                break;
            case R.id.iv_right_faceud:
                moveUpOrDown(true);
                break;
            case R.id.iv_left_scale:
                faceScaleChange(false);
                break;
            case R.id.iv_right_scale:
                faceScaleChange(true);
                break;
        }
    }

    private void faceScaleChange(boolean b) {
        float scale = SpUtils.getFaceScale();
        if (b) scale += 0.1f;
        else scale -= 0.1f;
        scale = new BigDecimal(scale).setScale(1, BigDecimal.ROUND_HALF_UP).floatValue();
        if (scale < 0.5f || scale > 1.5f) {
            ToastUtils.showToast("超出范围");
            return;
        }
        SpUtils.setFaceScale(scale);
        tv_scale_value.setText(String.valueOf(SpUtils.getFaceScale()));
    }

    private void moveUpOrDown(boolean b) {
        int move = SpUtils.getMoveUpOrDown();
        if (b) move += 5;
        else move -= 5;
        if (move < -200 || move > 200) {
            ToastUtils.showToast("超出范围");
            return;
        }
        SpUtils.setMoveUpOrDown(move);
        tv_faceud_value.setText(String.valueOf(SpUtils.getMoveUpOrDown()));
    }

    private void moveLeftOrRight(boolean b) {
        int move = SpUtils.getMoveLeftOrRight();
        if (b) move += 5;
        else move -= 5;
        if (move < -200 || move > 200) {
            ToastUtils.showToast("超出范围");
            return;
        }
        SpUtils.setMoveLeftOrRight(move);
        tv_facelf_value.setText(String.valueOf(SpUtils.getMoveLeftOrRight()));
    }

    private void disChange(boolean b) {
        int dis = SpUtils.getFaceDistance();
        int progress = selectProgressByDistance(dis);
        if (b) progress += 1;
        else progress -= 1;
        if (progress < 0 || progress > 7){
            ToastUtils.showToast("超出范围");
            return;
        }
        SpUtils.setFaceDistance(selectDistanceByProgress(progress));
        showFaceDistance(SpUtils.getFaceDistance());
    }

    private void showFaceDistance(int dis){
        switch (selectProgressByDistance(dis)) {
            case 0:
                tv_dis_value.setText("40cm");
                break;
            case 1:
                tv_dis_value.setText("60cm");
                break;
            case 2:
                tv_dis_value.setText("80cm");
                break;
            case 3:
                tv_dis_value.setText("100cm");
                break;
            case 4:
                tv_dis_value.setText("120cm");
                break;
            case 5:
                tv_dis_value.setText("140cm");
                break;
            case 6:
                tv_dis_value.setText("200cm");
                break;
            case 7:
                tv_dis_value.setText("280cm");
                break;
        }
    }

    private int selectProgressByDistance(int distance) {
        switch (distance) {
            case 4:
                return 0;
            case 5:
                return 1;
            case 6:
                return 2;
            case 7:
                return 3;
            case 8:
                return 4;
            case 9:
                return 5;
            case 12:
                return 6;
            case 16:
                return 7;
            default:
                return 4;
        }
    }

    private int selectDistanceByProgress(int progress) {
        switch (progress) {
            case 0:
                return 4;
            case 1:
                return 5;
            case 2:
                return 6;
            case 3:
                return 7;
            case 4:
                return 8;
            case 5:
                return 9;
            case 6:
                return 12;
            case 7:
                return 16;
            default:
                return 8;
        }
    }

    private void moreChange(boolean b) {
        int more = SpUtils.getThreshold();
        if (b) more += 1;
        else more -= 1;
        if (more < 1 || more > 99) {
            ToastUtils.showToast("超出范围");
            return;
        }
        SpUtils.setThreshold(more);
        tv_more_value.setText(String.valueOf(SpUtils.getThreshold()));
    }
}
